<?PHP
$id = $_GET['id_user'];
include "url.php";
include_once "ini.php";
dbconnect();
// requete sur les données de l'utilisateur
$sql = "SELECT * FROM users WHERE id_user = '".$id."'";
$requete = mysql_query($sql);
if (!$requete)
{
    echo "SQL error : ", $sql;
    exit ;
}
else
{

    while ($users = mysql_fetch_assoc($requete))
    {
		// Recupération de derniers de coordonnes de l'utilisateur pour retour via json
        $json = $users;
        $coord = split(',', $users['position_cam']); // coordonnes de l'utilisateur camera
        $distance = 15000;
        $xmin = ((int)$coord[0])-$distance;
        $xmax = ((int)$coord[0])+$distance;
        $zmin = ((int)$coord[2])-$distance;
        $zmax = ((int)$coord[2])+$distance;
		// requete SQL permettant de chercher les images proches de l'utilisateur.
        //$sql = "SELECT * FROM images WHERE coord_x < '".$xmax."' AND coord_x > '".$xmin."' AND coord_z < '".$zmax."' AND coord_z > '".$zmin."' AND droit = 'public';";
		$sql = "SELECT * FROM images WHERE coord_x < '".$xmax."' AND coord_x > '".$xmin."' AND coord_z < '".$zmax."' AND coord_z > '".$zmin."' AND droit = 'public' AND posted = 'oui';";

		$publicImage = mysql_query($sql);
		
		//---------------//
		// Ecriture des fichiers ImagePublic.txt et imagePrive.txt ecrivant les images proches 
		//---------------//
	
		// imagePublic.txt
		if (!$publicImage)
		{
		    echo "SQL error : ", $sql;
		    exit ;
		}
		else
		{
		    $filePublic = "../imagePublic.txt";
		    $fpPublic = fopen($filePublic, "w");
		    while ($tab = mysql_fetch_assoc($publicImage))
		    {
		        $ligne = $tab['coord_x'].",".$tab['coord_y'].",".$tab['coord_z']."	".$tab['orientation_dir']."	".$tab['orientation_up']."	".$tab['nom_image']."	".$url."images/".$tab['nom_image']."	".$tab['id_image']."\n";
			fputs($fpPublic, $ligne);
		    }
		    fclose($fpPublic);
		}


		$sql2 = "SELECT * FROM images WHERE coord_x < '".$xmax."' AND coord_x > '".$xmin."' AND coord_z < '".$zmax."' AND coord_z > '".$zmin."' AND droit = 'private' AND posted = 'oui';";
		$privateImage = mysql_query($sql2);

		// imagePrive.txt
		if (!$privateImage)
		{
		    echo "SQL error : ", $sql2;
		    exit ;
		}
		else
		{
		    $filePrivate = "../imagePrive.txt";
		    $fpPrivate = fopen($filePrivate, "w");
	
		    while ($tab = mysql_fetch_assoc($privateImage))
		    {
				$ligne = $tab['coord_x'].",".$tab['coord_y'].",".$tab['coord_z']."	".$tab['orientation_dir']."	".$tab['orientation_up']."	".$tab['nom_image']."	".$url."images/".$tab['nom_image']."	".$tab['id_image']."\n"; 
				fputs($fpPrivate, $ligne);
		    }
		    fclose($fpPrivate);
		}
    	echo json_encode($json);
	}
}
dbclose();
?>
